// // 创建 WebSocket 连接
const { WebSocket, WebSocketServer } = require('ws');
// import { WebSocketServer } from 'ws';

// // 创建 WebSocket 服务器
const wss = new WebSocket.Server({ port: 3300 });

// 监听连接事件
wss.on('connection', function connection(ws) {
    ws.on('error', console.error);

    ws.on('message', function message(data) {
        console.log('received: %s', data);
        wss.clients.forEach(function each(client) {
            if (client !== ws && client.readyState === WebSocket.OPEN) {
                client.send(data); // 向所有连接的客户端广播消息
            }
        });
    });

    ws.send(JSON.stringify({ message: 'something' }));
});

console.log('WebSocket 服务器已启动，监听端口 3300');


